home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / toogl / irisgl_light.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  9KB  |  374 lines

  1. /*
  2.  * Copyright (c) 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or publicity relating 
  9.  * to the software without the specific, prior written permission of 
  10.  * Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, 
  14.  * ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  15.  *
  16.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, 
  17.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER 
  18.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF 
  19.  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT 
  20.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. /*
  24.  *  irisgl_light.c
  25.  *  $Revision: 1.1 $
  26.  *
  27.  *    some simple functions to make porting lighting code from
  28.  *    iris to open gl easier.
  29.  *
  30.  */
  31.  
  32. #include <GL/gl.h>
  33. #include <gl/gl.h> /* for iris gl lighting constants */
  34. #include <stdio.h>
  35. #include <math.h>
  36.  
  37. /* exports */
  38. void mylmdef(short deftype, short index, short np, float props[]);
  39. void mylmbind(short target, short index);
  40.  
  41. /* internal */
  42. static void mylmdef_material(short index, float props[]);
  43. static void mylmdef_lmodel(short index, float props[]);
  44. static void mylmdef_light(short index, float props[]);
  45. static void mylmbind_material(int index);
  46. static void mylmbind_lmodel(int index);
  47. static void mylmbind_light(int target,int index);
  48.  
  49.  
  50. #define MAXMAT 10
  51. #define MAXLM 2
  52. #define MAXLT 10
  53.  
  54. typedef float _GLfloat;
  55.  
  56. typedef struct {
  57.    _GLfloat x, y, z, w;
  58. } _GLcoord;
  59.  
  60. typedef struct {
  61.    _GLfloat r, g, b, a;
  62. } _GLcolor;
  63.  
  64. typedef struct {
  65.     int defined;
  66.     _GLcolor ambient;
  67.     _GLcolor diffuse;
  68.     _GLcolor specular;
  69.     _GLcolor emissive;
  70.     _GLfloat specularExponent;
  71.     _GLfloat cmapa, cmaps, cmapd; /* not used */
  72.     int aflag,dflag,sflag,eflag,expflag; /* set if this prop is set by iris gl */
  73. } Material;
  74.  
  75. static Material mat[MAXMAT];
  76.  
  77. typedef struct {
  78.     int defined;
  79.     _GLcolor ambient;
  80.     int localViewer;
  81.     int twoSided;
  82.     int aflag,lflag,tflag; /* set if this prop is set by iris gl */
  83. } LightModel;
  84.  
  85. static LightModel lm[MAXLM];
  86.  
  87. typedef struct {
  88.     int defined;
  89.     _GLcolor ambient;
  90.     _GLcolor diffuse;
  91.     _GLcolor specular; /* not used */
  92.     _GLcoord position;
  93.     _GLfloat spotLightExponent; /* not used */
  94.     _GLfloat spotLightCutOffAngle; /* not used */
  95.     _GLfloat constantAttenuation; /* not used */
  96.     _GLfloat linearAttenuation; /* not used */
  97.     _GLfloat quadraticAttenuation; /* not used */
  98.     int aflag,dflag,sflag,pflag; /* set if this prop is set by iris gl */
  99. } LightSource;
  100.  
  101. static LightSource lt[MAXLT];
  102.  
  103. static material_enable,lmodel_enable; /* used to determine if lighting is on/off */
  104.  
  105.  
  106. void
  107. mylmdef(short deftype, short index, short np, float props[])
  108. {
  109.     switch (deftype) {
  110.       case DEFMATERIAL:
  111.     mylmdef_material(index,props);
  112.     break;
  113.       case DEFLMODEL:
  114.     mylmdef_lmodel(index,props);
  115.     break;
  116.       case DEFLIGHT:
  117.     mylmdef_light(index,props);
  118.     break;
  119.     }
  120. }
  121.  
  122. void
  123. mylmbind(short target, short index)
  124. {
  125.     switch (target) {
  126.       case MATERIAL:
  127.     mylmbind_material(index);
  128.     break;
  129.       case LMODEL:
  130.     mylmbind_lmodel(index);
  131.     break;
  132.       default: /* LIGHTn */
  133.     mylmbind_light(target,index);
  134.     break;
  135.     }
  136. }
  137.  
  138. /*
  139.  *  XXX - defaults of iris and open gl may be different
  140.  */
  141. static void
  142. mylmdef_material(short index, float props[])
  143. {
  144. int done;
  145. float prop;
  146. static char pre[] = "mylmdef_material:";
  147.  
  148.     if (index >= MAXMAT) {
  149.     fprintf(stderr,"%s index %d too big\n",pre,index);
  150.     return;
  151.     }
  152.  
  153.     /* if not yet defined, set some defaults */
  154.     if (!mat[index].defined) {
  155.     mat[index].defined = 1;
  156.     }
  157.  
  158.  
  159.     /* parse iris gl props */
  160.     done = 0;
  161.     while (!done) {
  162.     switch ((int)(prop = *props++)) {
  163.       case AMBIENT:
  164.         mat[index].ambient.r = *props++;
  165.         mat[index].ambient.g = *props++;
  166.         mat[index].ambient.b = *props++;
  167.         mat[index].ambient.a = 1.0;
  168.         mat[index].aflag = 1;
  169.         break;
  170.       case DIFFUSE:
  171.         mat[index].diffuse.r = *props++;
  172.         mat[index].diffuse.g = *props++;
  173.         mat[index].diffuse.b = *props++;
  174.         mat[index].diffuse.a = 1.0;
  175.         mat[index].dflag = 1;
  176.         break;
  177.       case SPECULAR:
  178.         mat[index].specular.r = *props++;
  179.         mat[index].specular.g = *props++;
  180.         mat[index].specular.b = *props++;
  181.         mat[index].specular.a = 1.0;
  182.         mat[index].sflag = 1;
  183.         break;
  184.       case EMISSION:
  185.         mat[index].emissive.r = *props++;
  186.         mat[index].emissive.g = *props++;
  187.         mat[index].emissive.b = *props++;
  188.         mat[index].emissive.a = 1.0;
  189.         mat[index].eflag = 1;
  190.         break;
  191.       case SHININESS:
  192.         mat[index].specularExponent = *props++;
  193.         mat[index].expflag = 1;
  194.         break;
  195.       case ALPHA:
  196.         props++;
  197.         fprintf(stderr,"%s ALPHA ignored\n",pre);
  198.         break;
  199.       case (int)LMNULL:
  200.         done = 1;
  201.         break;
  202.       default:
  203.         fprintf(stderr,"%s unexpected prop %g\n",pre,prop);
  204.         break;
  205.     }
  206.     }
  207. }
  208.  
  209. static void
  210. mylmbind_material(int index)
  211. {
  212.     material_enable = index;
  213.     if (index == 0) {
  214.     glDisable(GL_LIGHTING);
  215.     }
  216.     else {
  217.     if (material_enable && lmodel_enable) {
  218.         glEnable(GL_LIGHTING);
  219.     }
  220.     if (mat[index].aflag) 
  221.         glMaterialfv(GL_FRONT,GL_AMBIENT,&mat[index].ambient.r);
  222.     if (mat[index].dflag) 
  223.         glMaterialfv(GL_FRONT,GL_DIFFUSE,&mat[index].diffuse.r);
  224.     if (mat[index].sflag) 
  225.         glMaterialfv(GL_FRONT,GL_SPECULAR,&mat[index].specular.r);
  226.     if (mat[index].eflag) 
  227.         glMaterialfv(GL_FRONT,GL_EMISSION,&mat[index].emissive.r);
  228.     if (mat[index].expflag) 
  229.         glMaterialf(GL_FRONT,GL_SHININESS,mat[index].specularExponent);
  230.     }
  231. }
  232.  
  233.  
  234. static void
  235. mylmdef_lmodel(short index, float props[])
  236. {
  237. int done;
  238. float prop;
  239. static char pre[] = "mylmdef_lmodel:";
  240.  
  241.     if (index >= MAXLM) {
  242.     fprintf(stderr,"%s index %d too big\n",pre,index);
  243.     return;
  244.     }
  245.  
  246.     /* if not yet defined, set some defaults (XXX) */
  247.     if (!lm[index].defined) {
  248.     lm[index].defined = 1;
  249.     }
  250.  
  251.     /* parse iris gl props */
  252.     done = 0;
  253.     while (!done) {
  254.     switch ((int)(prop = *props++)) {
  255.       case AMBIENT:
  256.         lm[index].ambient.r = *props++;
  257.         lm[index].ambient.g = *props++;
  258.         lm[index].ambient.b = *props++;
  259.         lm[index].ambient.a = 1.0;
  260.         lm[index].aflag = 1;
  261.         break;
  262.       case LOCALVIEWER:
  263.         lm[index].localViewer = *props++;
  264.         lm[index].lflag = 1;
  265.         break;
  266.       case TWOSIDE:
  267.         lm[index].twoSided = *props++;
  268.         lm[index].tflag = 1;
  269.         break;
  270.       case (int)LMNULL:
  271.         done = 1;
  272.         break;
  273.       default:
  274.         fprintf(stderr,"%s unexpected prop %g\n",pre,prop);
  275.         break;
  276.     }
  277.     }
  278. }
  279.  
  280. static void
  281. mylmbind_lmodel(int index)
  282. {
  283.     lmodel_enable = index;
  284.     if (index == 0) {
  285.     glDisable(GL_LIGHTING);
  286.     }
  287.     else {
  288.     if (material_enable && lmodel_enable) {
  289.         glEnable(GL_LIGHTING);
  290.     }
  291.     if (lm[index].aflag) 
  292.         glLightModelfv(GL_LIGHT_MODEL_AMBIENT,&lm[index].ambient.r);
  293.     if (lm[index].lflag) 
  294.         glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER,lm[index].localViewer);
  295.     if (lm[index].tflag) 
  296.         glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,lm[index].twoSided);
  297.     }
  298. }
  299.  
  300. static void
  301. mylmdef_light(short index, float props[])
  302. {
  303. int done;
  304. float prop;
  305. static char pre[] = "mylmdef_light:";
  306.  
  307.     if (index >= MAXLT) {
  308.     fprintf(stderr,"%s index %d too big\n",pre,index);
  309.     return;
  310.     }
  311.  
  312.     /* if not yet defined, set some defaults (XXX) */
  313.     if (!lt[index].defined) {
  314.     lt[index].defined = 1;
  315.     }
  316.  
  317.     /* parse iris gl props */
  318.     done = 0;
  319.     while (!done) {
  320.     switch ((int)(prop = *props++)) {
  321.       case AMBIENT:
  322.         lt[index].ambient.r = *props++;
  323.         lt[index].ambient.g = *props++;
  324.         lt[index].ambient.b = *props++;
  325.         lt[index].ambient.a = 1.0;
  326.         lt[index].aflag = 1;
  327.         break;
  328.       case LCOLOR:
  329.         lt[index].diffuse.r = *props++;
  330.         lt[index].diffuse.g = *props++;
  331.         lt[index].diffuse.b = *props++;
  332.         lt[index].diffuse.a = 1.0;
  333.         lt[index].dflag = 1;
  334.         break;
  335.       case POSITION:
  336.         lt[index].position.x = *props++;
  337.         lt[index].position.y = *props++;
  338.         lt[index].position.z = *props++;
  339.         lt[index].position.w = *props++;
  340.         lt[index].pflag = 1;
  341.         break;
  342.       case (int)LMNULL:
  343.         done = 1;
  344.         break;
  345.       default:
  346.         fprintf(stderr,"%s unexpected prop %g\n",pre,prop);
  347.         break;
  348.     }
  349.     }
  350. }
  351.  
  352. static void 
  353. mylmbind_light(int target,int index)
  354. {
  355. int num;
  356.  
  357.     num = target-LIGHT0;
  358.     if (index == 0) { /* turn off this light */
  359.     glDisable(GL_LIGHT0+num);
  360.     }
  361.     else { /* turn on lighting */
  362.     glEnable(GL_LIGHT0+num);
  363.     if (lt[index].aflag) 
  364.         glLightfv(GL_LIGHT0+num,GL_AMBIENT,<[index].ambient.r);
  365.     /* iris gl has no specular light, so just use the same as diffuse */
  366.     if (lt[index].dflag) {
  367.         glLightfv(GL_LIGHT0+num,GL_DIFFUSE,<[index].diffuse.r);
  368.         glLightfv(GL_LIGHT0+num,GL_SPECULAR,<[index].diffuse.r);
  369.     }
  370.     if (lt[index].pflag) 
  371.         glLightfv(GL_LIGHT0+num,GL_POSITION,<[index].position.x);
  372.     }
  373. }
  374.